<?php
namespace Views;

require_once('\Models\Database.php');
require_once('\Common\DBTables.php');
require_once('\Models\User.php');
require_once('\Common\Strings.php');

class ManageUsersView
{
	public $userCreated 	= "userCreated";
	public $userDeleted 	= "userDeleted";
	public $tryCreateUser 	= "tryCreateUser";
	public $tryDeleteUser 	= "tryDeleteUser";
	
	private $_pv = null;
	
	public function __construct()
	{
		$this->_pv = new \Views\PageView();
	}
	
	/**
	 * Skapar registreringformulär
	 * @return String HTML-formulär
	 */
	public function getRegisterForm()
	{
		$dbt = new \Common\DBTables();
		
		$html = 
		"<form method='POST' action='manageUsers.php'>
			<fieldset>
				<div class='create-user-box'>
					<label for='".$this->_pv->userName."'>Användarnamn</label>
					<input type='text' id='".$this->_pv->userName."' name='".$this->_pv->userName."' /><br />
					<label for='".$this->_pv->firstName."'>Förnamn</label>
					<input type='text' id='".$this->_pv->firstName."' name='".$this->_pv->firstName."' /><br />
					<label for='".$this->_pv->lastName."'>Efternamn</label>
					<input type='text' id='".$this->_pv->lastName."' name='".$this->_pv->lastName."' /><br />
					<label for='".$this->_pv->email."'>E-post</label>
					<input type='email' id='".$this->_pv->email."' name='".$this->_pv->email."' /><br />
					<label for='".$this->_pv->password."'>Lösenord</label>
					<input type='password' id='".$this->_pv->password."' name='".$this->_pv->password."' /><br />
					<label for='".$this->_pv->repeatPassword."'>Repetera lösenord</label>
					<input type='password' id='".$this->_pv->repeatPassword."' name='".$this->_pv->repeatPassword."' /><br />
					<input type='hidden' name='".$this->tryCreateUser."' /><br />
				</div>
					<p><em>".\Common\Strings::TEXT_PASSWORD_INFO."</em></p>
					<input type='submit' value='Spara' />
			</fieldset>
		</form>";
		
		return $html;
	}
	
	/**
	 * Returnerar en lista över databasens användare
	 * @param $allUsers UserArray
	 * @return $html HTML-string
	 */
	public function getUsersList(\Models\UserArray $allUsers)
	{
		$html ="";
		
		// Loopar igenom användarna och skriver ut info
		foreach ($allUsers->get() as $user)
		{
			$html .=  
			"<form method='post' OnSubmit=\"return confirm('".\Common\Strings::CONFIRM_DELETE_USER."');\">
				<span>".$user->FirstName()." ".$user->LastName()." (".$user->UserName().")</span>
				<input type='hidden' name='".$this->_pv->userId."' value='".$user->UserId()."' />
				<input type='hidden' name='".$this->tryDeleteUser."' />
				<input type='submit' value='Ta bort' id='user".$user->UserId()."' />
			</form>";
		}
		
		return $html;
	}

	/**
	 * Kollar om användaren har skapat ny användare
	 * @return Bool
	 */
	public function userCreated()
	{
		if(isset($_GET[$this->_pv->status]) && $_GET[$this->_pv->status] == $this->userCreated)
			return true;
		else
			return false;
	}
	
	/**
	 * Kollar om användaren har tagit bort en användare
	 * @return Bool
	 */
	public function userDeleted()
	{
		if(isset($_GET[$this->_pv->status]) && $_GET[$this->_pv->status] == $this->userDeleted)
			return true;
		else
			return false;
	}
}

?>